package org.dromara.bykt.service.impl;

import org.dromara.bykt.domain.vo.BYKTExamInfoImportVo;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bykt.domain.bo.BYKTExamInfoBo;
import org.dromara.bykt.domain.vo.BYKTExamInfoVo;
import org.dromara.bykt.domain.BYKTExamInfo;
import org.dromara.bykt.mapper.BYKTExamInfoMapper;
import org.dromara.bykt.service.IBYKTExamInfoService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 考试咨询Service业务层处理
 *
 * @author Hou
 * @date 2025-07-02
 */
@RequiredArgsConstructor
@Service
public class BYKTExamInfoServiceImpl implements IBYKTExamInfoService {

    private final BYKTExamInfoMapper baseMapper;

    /**
     * 查询考试咨询
     *
     * @param id 主键
     * @return 考试咨询
     */
    @Override
    public BYKTExamInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询考试咨询列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 考试咨询分页列表
     */
    @Override
    public TableDataInfo<BYKTExamInfoVo> queryPageList(BYKTExamInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BYKTExamInfo> lqw = buildQueryWrapper(bo);
        Page<BYKTExamInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的考试咨询列表
     *
     * @param bo 查询条件
     * @return 考试咨询列表
     */
    @Override
    public List<BYKTExamInfoVo> queryList(BYKTExamInfoBo bo) {
        LambdaQueryWrapper<BYKTExamInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BYKTExamInfo> buildQueryWrapper(BYKTExamInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BYKTExamInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BYKTExamInfo::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BYKTExamInfo::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), BYKTExamInfo::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), BYKTExamInfo::getContent, bo.getContent());
        lqw.eq(bo.getAffiliationIp() != null, BYKTExamInfo::getAffiliationIp, bo.getAffiliationIp());
        lqw.eq(StringUtils.isNotBlank(bo.getInformationType()), BYKTExamInfo::getInformationType, bo.getInformationType());
        return lqw;
    }

    /**
     * 新增考试咨询
     *
     * @param bo 考试咨询
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BYKTExamInfoBo bo) {
        bo.setCreateTime(new Date());
        BYKTExamInfo add = MapstructUtils.convert(bo, BYKTExamInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改考试咨询
     *
     * @param bo 考试咨询
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BYKTExamInfoBo bo) {
        BYKTExamInfo update = MapstructUtils.convert(bo, BYKTExamInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BYKTExamInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除考试咨询信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public BYKTExamInfoVo selectText(String text) {
        BYKTExamInfoVo byktExamInfoVo = baseMapper.selectVoOne(new LambdaQueryWrapper<BYKTExamInfo>().eq(BYKTExamInfo::getTitle, text));
        return byktExamInfoVo;
    }
}
